﻿Public Class frmRegCardio

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.Close()
    End Sub


    Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
        If cmbMaquina.Text = "" Then
            MsgBox("Debe seleccionar una maquina", MsgBoxStyle.Exclamation)
            Exit Sub
        End If
        If IsNumeric(txtCalorias.Text) = False Or IsNumeric(txtDistancia.Text) = False Or IsNumeric(txtMinutos.Text) = False Then
            MsgBox("Todos los Valores deben ser numericos", MsgBoxStyle.Exclamation)
            Exit Sub
        End If

        Dim comando As New OleDbCommand
        Dim texto As String

        comando.Connection = Conexion

        texto = "insert into cardio (usuario, fecha, maquina, minutos, distancia, calorias) values ('"
        texto &= Usuario & "', '"
        texto &= dtpFecha.Value.ToShortDateString & "', '"
        texto &= cmbMaquina.Text & "', '"
        texto &= txtMinutos.Text & "', '"
        texto &= txtDistancia.Text & "', '"
        texto &= txtCalorias.Text & "')"
        comando.CommandText = texto
        comando.Connection = Conexion
        Try
            comando.ExecuteNonQuery()
            MsgBox("Registro Agregado", MsgBoxStyle.Information)
            CargarTabla()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try



    End Sub
    Sub CargarComboMaquinas()

        Dim data As New DataSet
        Dim comando As New OleDbCommand
        Dim adaptador As New OleDbDataAdapter
        Dim fila As DataRow

        'Grid1.Rows.Clear()

        comando.CommandText = "Select maquina from maquinas order by id"
        comando.Connection = Conexion
        adaptador.SelectCommand = comando
        adaptador.Fill(data, "tabla")

        For Each fila In data.Tables("tabla").Rows
            cmbMaquina.Items.Add(fila.Item("Maquina"))
        Next
    End Sub

    Private Sub frmRegCardio_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CargarComboMaquinas()
        CargarTabla()
    End Sub
    Sub CargarTabla()
        Dim data As New DataSet
        Dim adaptador As New OleDb.OleDbDataAdapter
        Dim comando As New OleDb.OleDbCommand
        Dim fecha As String

        fecha = DateAdd(DateInterval.Day, -30, Now).ToShortDateString

        comando.Connection = Conexion

        comando.CommandText = "Select * from cardio where Fecha>#" & fecha & "# order by fecha"
        adaptador.SelectCommand = comando
        adaptador.Fill(data, "tabla")

        'dgvMaquinas.Rows.Clear()
        dgvHistorial.DataSource = data
        dgvHistorial.DataMember = "tabla"
        dgvHistorial.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        dgvHistorial.ReadOnly = True
        dgvHistorial.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dgvHistorial.Columns(0).Visible = False
        dgvHistorial.Columns(6).Visible = False
        dgvHistorial.Refresh()
        Graficar(3)



    End Sub

    Function Graficar(ByVal Numero As Integer)
        Dim i As Integer
        Chart1.Series(0).Points.Clear()
        Select Case Numero
            Case Is = 3
                Chart1.Series(0).Name = "Minutos"
            Case Is = 4
                Chart1.Series(0).Name = "Distancia"
            Case Is = 5
                Chart1.Series(0).Name = "Calorias"
        End Select

        For i = 0 To dgvHistorial.RowCount - 2
            Chart1.Series(0).Points.AddXY(dgvHistorial.Rows(i).Cells(1).Value, dgvHistorial.Rows(i).Cells(Numero).Value)
        Next
        Return True
    End Function


    Private Sub btnMinutos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinutos.Click
        Graficar(3)
    End Sub

    Private Sub btnDistancia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDistancia.Click
        Graficar(4)
    End Sub

    Private Sub btnCalorias_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalorias.Click
        Graficar(5)
    End Sub
End Class